<template>
  <div class="role">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>筛选搜索</span>
      </div>
      <el-form ref="form" :inline="true" :model="form" class="demo-form-inline">
        <el-form-item prop="name" label="输入搜索">
            <el-input v-model="form.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit" :disabled="disabled">查询</el-button>
            <el-button @click="onReset" :disabled="disabled">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button
        style="margin-bottom:20px"
        size="mini"
        @click="onAddRole()" :disabled="disabled">添加角色
      </el-button>
      <el-table
        v-loading="disabled"
        :data="roles"
        border
        style="width: 100%">
        <el-table-column
        fixed
        prop="id"
        label="编号"
        width="60"
        >
        </el-table-column>
        <el-table-column
        prop="name"
        label="姓名"
        >
        </el-table-column>
        <el-table-column
        prop="description"
        label="描述"
        >
        </el-table-column>
        <el-table-column
        prop="createdTime"
        label="添加时间"
        >
        </el-table-column>
        <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="160">
        <template slot-scope="scope">
            <el-row>
                <el-button
                  type="text"
                  size="small"
                  @click="$router.push({
                    name: 'alloc-menu',
                    params: {
                      roleId: scope.row.id
                    }
                  })"
                >分配菜单</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="$router.push({
                    name: 'alloc-resource',
                    params: {
                      roleId: scope.row.id
                    }
                  })"
                >分配资源</el-button>
            </el-row>
            <el-row>
                <el-button type="text" @click="onEditRole(scope.row)" size="small">编辑</el-button>
                <el-button type="text" @click="onDeleteRole(scope.row)" size="small">删除</el-button>
            </el-row>
        </template>
        </el-table-column>
    </el-table>
    </el-card>
    <el-dialog
      :title="isEdit ? '编辑角色' : '添加角色'"
      :visible.sync="dialogVisible"
      width="500px"
      >
      <create-or-edit
        v-if="dialogVisible"
        :roleId="roleId"
        :isEdit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible=false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllRole, getRole, deleteRole } from '@/services/role'
import { getTime } from '@/utils/time'
import { Form } from 'element-ui'
import CreateOrEdit from './CreateOrEdit.vue'

export default Vue.extend({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      form: {
        name: ''
      },
      roleId: null,
      isEdit: false,
      isLoading: false,
      dialogVisible: false,
      disabled: true,
      roles: [] // 用户列表
    }
  },
  created () {
    this.loadAllRoles()
  },
  methods: {
    async loadAllRoles () {
      this.disabled = true
      const { data } = await getAllRole()
      this.roles = data.data.reverse().map((item: any) => {
        item.createdTime = getTime(item.createdTime)
        return item
      })
      this.disabled = false
    },
    async onSubmit () {
      const { data } = await getRole(this.form)
      if (data.code === '000000') {
        this.roles = data.data.records.reverse().map((item: any) => {
          item.createdTime = getTime(item.createdTime)
          return item
        })
      }
    },
    async onReset () {
      await (this.$refs.form as Form).resetFields()
      this.loadAllRoles()
    },
    onAddRole () {
      this.dialogVisible = true
      this.isEdit = false
    },
    onEditRole (role: any) {
      this.dialogVisible = true
      this.isEdit = true
      this.roleId = role.id
    },
    onDeleteRole (role: any) {
      this.$confirm('确认删除用户？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteRole(role.id)
        this.$message.success('删除成功')
        this.loadAllRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    onSuccess () {
      this.dialogVisible = false
      this.loadAllRoles()
    }
  }
})
</script>

<style lang="scss" scoped></style>
